package org.yookiee.support.core.datasource.local.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Service;
import org.yookiee.support.constants.I18nConstant;
import org.yookiee.support.core.KeyExpression;
import org.yookiee.support.core.SysI18nAllocate;
import org.yookiee.support.core.datasource.local.LocalDatasource;
import org.yookiee.support.enums.DatasourceType;
import org.yookiee.support.enums.Scope;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Map;

/**
 * FileName LocalDatasourceImpl
 * Package org.yookiee.support.datasource.local.impl
 * Description
 * author cpstory
 * create 2023-11-30 14:31
 * version V1.0
 */
@Slf4j
@Service
@ConditionalOnProperty(name = "easy-i18n.datasource",havingValue = "local")
public class LocalDatasourceImpl implements LocalDatasource {
    @Resource
    Environment environment;
    private Map<String,Map<String,SysI18nAllocate>> localCachedMap = Maps.newHashMap();

    @PostConstruct
    public void init(){
        loadCache();
    }

    @Override
    public Map<String,SysI18nAllocate> getScopeData(String scope) {
        if(Strings.isNullOrEmpty(scope)){
            return Maps.newHashMap();
        }
        Scope formattedScope = Scope.format(scope);
        if(formattedScope == null){
            return Maps.newHashMap();
        }
        return localCachedMap.get(formattedScope.getScope());
    }

    @Override
    public void loadCache() {
        long startTime = System.nanoTime();
        log.info("easy-i18n: start to load local data");
        for (PropertySource<?> propertySource : ((ConfigurableEnvironment) environment).getPropertySources()) {
            if (propertySource instanceof EnumerablePropertySource) {
                for (String name : ((EnumerablePropertySource<?>) propertySource).getPropertyNames()) {
                    if (name.startsWith("easy-i18n.java") || name.startsWith("easy-i18n.web")) {
                        log.info("name: {} , properties: {}", name, propertySource.getProperty(name));
                        SysI18nAllocate adapt = KeyExpression.adapt(name, (String) propertySource.getProperty(name));
                        final String key = adapt.getModules() + I18nConstant.SPOT + adapt.getLabels();
                        if (!localCachedMap.containsKey(adapt.getScope())) {
                            Map<String,SysI18nAllocate> scopeMap = Maps.newHashMap();
                            //key处理成表达式
                            scopeMap.put(key,adapt);
                            localCachedMap.put(Scope.format(adapt.getScope()).getScope(),scopeMap);
                        }else{
                            Map<String, SysI18nAllocate> scopeMap = localCachedMap.get(adapt.getScope());
                            //key处理成表达式
                            scopeMap.put(key,adapt);
                        }

                    }
                }
            }
        }
        Duration timeTakenToLoad = Duration.ofNanos(System.nanoTime() - startTime);
        log.info("easy-i18n: local data load success in {} ms",timeTakenToLoad.getNano()/1000000);

    }
}
